package cn.itlight.org.controller;

import cn.itlight.basic.annotation.PreAuthorize;
import cn.itlight.basic.utils.AjaxResult;
import cn.itlight.basic.utils.LoginContext;
import cn.itlight.basic.utils.PageList;
import cn.itlight.org.domain.Department;
import cn.itlight.org.domain.Employee;
import cn.itlight.org.query.EmployeeQuery;
import cn.itlight.org.service.IEmployeeService;
import cn.itlight.pet.domain.Pet;
import cn.itlight.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @Description: controller层
* @Author: TomLiu
* @Version: 1.0
* @Date:  2022/6/24 10:07
*/
@RestController
@RequestMapping("/employee")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /*
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/24 10:07
    * @Parameters:
    * @Return
    */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    @PreAuthorize(name = "列表员工", sn = "employee:list")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /*
    * @Title: loadById
    * @Description: 根据id查询
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/24 10:07
    * @Parameters:
    * @Return
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "查询员工", sn = "employee:get")
    public Employee loadById(@ApiParam(value="ID属性",required = true) @PathVariable Long id){
        return employeeService.loadById(id);
    }

    /*
    * @Title: remove
    * @Description: 根据id删除
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/24 10:59
    * @Parameters:
    * @Return
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除员工", sn = "employee:delete")
    public AjaxResult remove(@PathVariable Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }

    /*
    * @Title: addOrUpt
    * @Description: 新增/修改
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/24 11:03
    * @Parameters:
    * @Return
    */
    @PutMapping
    @PreAuthorize(name = "增加或修改员工", sn = "employee:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        System.out.println(employee.getDepartment());
        try {
            if(employee.getId() == null){
                employeeService.save(employee);
            } else{
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }

    /*
    * @Title: addOrUpt
    * @Description: 高级查询/分页
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/24 11:05
    * @Parameters:
    * @Return
    */
    @PostMapping
    @PreAuthorize(name = "高级查询员工", sn = "employee:listQuery")
    public PageList queryData(@RequestBody EmployeeQuery employee){
        return employeeService.queryData(employee);
    }

    /*
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/6/26 10:33
    * @Parameters:
    * @Return
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除员工", sn = "employee:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            employeeService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }



    @GetMapping("/list/{shopId}")
    public List<Employee> addOrUpdate(@PathVariable Long shopId) {

        return employeeService.loadByShopId(shopId);
    }

}
